package com.digitalchina.lxnsy.activity.old;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.adapter.SimilarPesticideAdapter;
import com.digitalchina.lxnsy.api.BusinessPest;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.PesticideVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.widget.MyListView;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

import butterknife.Bind;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：byd
 * 创建时间：2017/4/5 15:18
 * 修改备注：
 * ================================================
 */

public class PesticideDetailsActivity extends BaseActivity {

    @Bind(R.id.act_pesticide_details_tv_title)
    TextView actPesticideDetailsTvTitle;
    @Bind(R.id.act_pesticide_details_sdv_image)
    SimpleDraweeView actPesticideDetailsSdvImage;
    @Bind(R.id.act_pesticide_details_tv_type)
    TextView actPesticideDetailsTvType;
    @Bind(R.id.act_pesticide_details_tv_certificate_no)
    TextView actPesticideDetailsTvCertificateNo;
    @Bind(R.id.act_pesticide_details_tv_effective_ingredients)
    TextView actPesticideDetailsTvEffectiveIngredients;
    @Bind(R.id.act_pesticide_details_tv_control_object)
    TextView actPesticideDetailsTvControlObject;
    @Bind(R.id.act_pesticide_details_tv_manufacturer)
    TextView actPesticideDetailsTvManufacturer;
    @Bind(R.id.act_pesticide_details_tv_product_performance)
    TextView actPesticideDetailsTvProductPerformance;
    @Bind(R.id.my_list_view)
    MyListView myListView;

    private SimilarPesticideAdapter mAdapter;

    @Override
    public void setLayout() {
        setTitle("农药详情");
        setContentLayout(R.layout.activity_pesticide_details);
    }

    @Override
    public void initMember() {
        if (getIntent().hasExtra("pesticideVO")) {
            PesticideVO pesticideVO = (PesticideVO) getIntent().getSerializableExtra("pesticideVO");
            getSimilarPesticide(pesticideVO.getId());
            setContent(pesticideVO);
        } else {
            getSimilarPesticide("2962"); //TODO 测试 真实数据后 删除
        }

    }

    /**
     * 填充数据
     */
    private void setContent(PesticideVO pesticideVO) {
        if (null != pesticideVO) {
            actPesticideDetailsTvTitle.setText(null == pesticideVO.getName() ? "" : pesticideVO.getName());
            if (StringUtil.isStrEmpty(pesticideVO.getImagePath())) {
                actPesticideDetailsSdvImage.setVisibility(View.GONE);
            } else {
                actPesticideDetailsSdvImage.setVisibility(View.VISIBLE);
                FrescoUtil.showImageSmall(pesticideVO.getImagePath(), actPesticideDetailsSdvImage);
            }
            actPesticideDetailsTvType.setText(null == pesticideVO.getPesticideType() ? "暂无数据" : StringUtil.isStrEmpty(pesticideVO.getPesticideType().getName()) ? "暂无数据" : pesticideVO.getPesticideType().getName());
            actPesticideDetailsTvCertificateNo.setText(StringUtil.isStrEmpty(pesticideVO.getRegistration()) ? "暂无数据" : pesticideVO.getRegistration());
            actPesticideDetailsTvEffectiveIngredients.setText(StringUtil.isStrEmpty(pesticideVO.getIngredient()) ? "暂无数据" : pesticideVO.getIngredient());
            actPesticideDetailsTvControlObject.setText(StringUtil.isStrEmpty(pesticideVO.getPrevention()) ? "暂无数据" : pesticideVO.getPrevention());
            actPesticideDetailsTvManufacturer.setText(StringUtil.isStrEmpty(pesticideVO.getManufacturer()) ? "暂无数据" : pesticideVO.getManufacturer());
            actPesticideDetailsTvProductPerformance.setText(StringUtil.isStrEmpty(pesticideVO.getDetail()) ? "暂无数据" : pesticideVO.getDetail());
        }
    }

    /**
     * 获取相似农药
     */
    private void getSimilarPesticide(String id) {
        BusinessPest.getPesticideDetail(this, id, mHandler);
    }

    @Override
    public void addListener() {
        myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                GotoUtil.gotoActivity(PesticideDetailsActivity.this, PesticideDetailsActivity.class, true, "pesticideVO", mAdapter.getListData().get(position));
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_PESTICIDE_DETAIL_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_GET_PESTICIDE_DETAIL_FIELD:
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        List<PesticideVO> pesticideVOList = FastJsonUtil.getListBean(object.toString(), "body", "pesticideList", PesticideVO.class);
        if (null != pesticideVOList) {
            mAdapter = new SimilarPesticideAdapter(this, pesticideVOList);
            myListView.setAdapter(mAdapter);
        }
    }

    @Override
    public void rightOnClick() {

    }

}
